package com.zhika.zishi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.zhika.zishi.R;
import com.zhika.zishi.adapter.SubmitStrategyAdapter;
import com.zhika.zishi.beans.SearchBean;
import com.zhika.zishi.customview.TitleView;
import com.zhika.zishi.interfaces.TitleInterface;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/27.
 * ListView的数据源是searchBean
 */
public class SubmitStrategyActivity extends BaseActivity {

    private ListView listView;

    private ArrayList<SearchBean> oldStrategyList = new ArrayList<>();

    private SubmitStrategyAdapter submitStrategyAdapter;

    //记录listView被点击的item
    private ImageView lastImageView = null;

    private TitleView titleView;

    private Button newStrategyButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.submit_strategy_layout);

        addTestData();

        initView();

    }

    private void initView(){
        listView = (ListView) findViewById(R.id.submit_strategy_list_view);
        submitStrategyAdapter = new SubmitStrategyAdapter(SubmitStrategyActivity.this,oldStrategyList);
        listView.setAdapter(submitStrategyAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (lastImageView!= null){
                    lastImageView.setVisibility(View.GONE);
                }

                lastImageView = (ImageView) view.findViewById(R.id.item_of_submit_strategy_tab_image_view);
                lastImageView.setVisibility(View.VISIBLE);
            }
        });


        titleView = (TitleView) findViewById(R.id.submit_strategy_title_view);
        titleView.setLeftButtonText("返回");
        titleView.setMiddleRowText("提交");
        titleView.setRightButtonText("发布");
        titleView.setTitleViewInterface(new TitleInterface() {
            @Override
            public void leftTitleListener() {
                finish();
            }

            @Override
            public void middleTitleListener() {

            }

            @Override
            public void rightTitleListener() {
                completeSubmit();
            }
        });

        newStrategyButton = (Button) findViewById(R.id.submit_strategy_new_strategy_button);
        newStrategyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                completeSubmit();
            }
        });

    }

    private void addTestData(){
       for (int i = 0;i<5;i++){
           SearchBean bean = new SearchBean();
           bean.setAnswerType("专栏攻略：");
           bean.setContentTitle("攻略标题");
           oldStrategyList.add(bean);
       }
    }

    /**
     * 完成提交攻略的最后一步，跳转到另一个界面
     */
    private void completeSubmit(){
        Intent intent = new Intent(SubmitStrategyActivity.this,SubmitStrategy2Activity.class);
        startActivity(intent);
    }
}
